import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutterdemo/2house730page/pages/property_detail_page/common.dart';
import 'package:flutterdemo/2house730page/widget/banner_search_widget.dart';
import 'package:flutterdemo/2house730page/widget/find_house_helper_item.dart';
import 'package:flutterdemo/2house730page/widget/house_list_item.dart';
import 'package:flutterdemo/2house730page/widget/middle_grid_item.dart';
import 'package:flutterdemo/2house730page/widget/top_icon_widget.dart';
import 'package:flutterdemo/2house730page/widget/top_search_widget.dart';

import 'event/events.dart';

void main() => runApp(MainPage());

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: MainPageWidget(),
      ),
    );
  }
}

class MainPageWidget extends StatefulWidget {
  @override
  _MainPageWidgetState createState() => _MainPageWidgetState();
}

class _MainPageWidgetState extends State<MainPageWidget> {
  var _scrollController = ScrollController();
  var _oldShowState = false;
  var _showTopSearch = false;
  double _oldScrollOffset = 0;
  double _currentScrollOffset = 0;

  bool _onNotification(ScrollNotification notification) {
    bool show = _scrollController.offset.h > 95.h;
    _currentScrollOffset = _scrollController.offset.h;
    if (_scrollController.offset.h <= 96.h) {
//      setState(() {
//        _scrollOffset = _scrollController.offset.h;
//      });
      if ((_currentScrollOffset - _oldScrollOffset).abs() > 2) {
        eventBus.fire(MainPageScrollEvent(_scrollController.offset.h));
        _oldScrollOffset = _currentScrollOffset;
      }
    }
    if (show != _oldShowState) {
      _oldShowState = show;
      setState(() {
        _showTopSearch = show;
      });
      eventBus.fire(MainPageScrollEvent(_scrollController.offset.h));
    }
    return true;
  }

  @override
  void initState() {
    super.initState();
    setStatusBarBlack();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 1080, height: 1920, allowFontScaling: false);
    return Stack(
      children: <Widget>[
        NotificationListener(
          onNotification: _onNotification,
          child: CustomScrollView(
            controller: _scrollController,
            slivers: <Widget>[
              SliverToBoxAdapter(
                child: BannerSearchWidget(
                  show: !_showTopSearch,
                  scrollOffset: 0,
                ),
              ),

              SliverPadding(
                padding: EdgeInsets.only(top: 15.h),
                sliver: SliverGrid(
                  //Grid
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 5,
//                    mainAxisSpacing: 10.h,
//                    crossAxisSpacing: 10.w,
                    childAspectRatio: 3.h,
                  ),
                  delegate: SliverChildBuilderDelegate(
                    (BuildContext context, int index) {
                      return MiddleGridItem(itemIndex: index);
                    },
                    childCount: 12,
                  ),
                ),
              ),

              SliverToBoxAdapter(
                child: Container(
                  margin: EdgeInsets.only(
                    left: 30.w,
                    top: 10.h,
                  ),
                  child: Text(
                    "搵楼好帮手",
                    style: TextStyle(fontSize: 50.sp, color: Colors.black, fontWeight: FontWeight.bold),
                  ),
                ),
              ),

              SliverToBoxAdapter(
                child: Container(
                  height: 140.h,
                  margin: EdgeInsets.only(top: 50.h),
                  child: ListView.builder(
                    itemBuilder: (context, index) {
                      return FindHouseHelperItem();
                    },
                    itemCount: 3,
                    scrollDirection: Axis.horizontal,
                  ),
                ),
              ),

              SliverToBoxAdapter(
                child: Container(
                  margin: EdgeInsets.only(left: 30.w, top: 30.h, right: 30.w),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text(
                        "售盘",
                        style: TextStyle(fontSize: 50.sp, color: Colors.black, fontWeight: FontWeight.bold),
                      ),
                      Container(
                        alignment: Alignment(0, 0),
                        height: 70.h,
                        width: 150.w,
                        decoration: new BoxDecoration(
                          color: Colors.white,
                          //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                          borderRadius: BorderRadius.all(Radius.circular(30.w)),
                          //设置四周边框
                          border: new Border.all(width: 1.w, color: Colors.grey),
                        ),
                        child: Text("更多 >"),
                      ),
                    ],
                  ),
                ),
              ),
              SliverToBoxAdapter(
                child: SizedBox(
                  height: 10.h,
                ),
              ),
              //List
              SliverFixedExtentList(
                itemExtent: 500.h,
                delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
//                  return Container(
//                    alignment: Alignment.center,
//                    color: Colors.lightBlue[100 * (index % 9)],
//                    child: Text('list item $index'),
//                  );
                  return HouseListItem();
                }, childCount: 20 //50个列表项
                    ),
              ),
            ],
          ),
        ),
        TopSearchWidget(
          show: _showTopSearch,
        ),
        TopIconWidget(
          show: _showTopSearch,
        ),
      ],
    );
  }
}
